package com.smartcruise.report;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.smartcruise.R;
import com.smartcruise.base.BaseTitleActivity;
import com.smartcruise.base.TitleBuilder;
import com.smartcruise.util.IntentUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by FengChaoQun
 * on 2018/1/24 0024
 * 录音界面
 */

public class AudioRecordActivity extends BaseTitleActivity {

    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.startRecord)
    Button startRecord;
    @BindView(R.id.cancel)
    Button cancel;
    @BindView(R.id.saveRecord)
    Button saveRecord;
    private long timeCount = 0;

    private int status; //0 没有开始录音 1正在录音 2结束录音

    private AudioRecordManager audioRecordManager;
    private boolean save;

    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            timeCount += 100;
            refreshTime();
            startCount();
        }
    };

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, AudioRecordActivity.class);
        activity.startActivityForResult(intent, IntentUtils.AUDIO_RECORD);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopCount();
        audioRecordManager.onDestroy();
        if (!save) {
            audioRecordManager.dropAudio();
        }
    }

    @Override
    protected void init() {
        audioRecordManager = new AudioRecordManager();
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_container_audio_record;
    }

    @Override
    protected TitleBuilder getTitleBuilder() {
        return TitleBuilder.create(true, getString(R.string.audioRecord));
    }

    private void refreshTime() {
        long minute = timeCount / (60 * 1000);
        long second = (timeCount - minute * 60 * 1000) / 1000;
        long endTime = (timeCount - minute * 60 * 1000 - second * 1000) / 100;
        String minuteString = minute < 10 ? "0" + minute : "" + minute;
        String secondString = second < 10 ? "0" + second : "" + second;
        String endTimeString = "" + endTime;
        time.setText(minuteString + ":" + secondString + ":" + endTimeString);
    }

    private void startCount() {
        handler.postDelayed(runnable, 100);
    }

    private void stopCount() {
        handler.removeCallbacks(runnable);
    }

    @OnClick(R.id.startRecord)
    public void onStartRecordClicked() {
        if (status == 0) {//开始录音
            startCount();
            status = 1;
            startRecord.setText("结束录音");
            saveRecord.setVisibility(View.GONE);

            audioRecordManager.startRecord();

        } else if (status == 1) {//结束录音
            stopCount();
            status = 2;
            startRecord.setText("重新录音");
            saveRecord.setVisibility(View.VISIBLE);

            audioRecordManager.endRecord();

        } else if (status == 2) {//重新录音
            timeCount = 0;
            startCount();
            status = 1;
            startRecord.setText("结束录音");
            saveRecord.setVisibility(View.GONE);

            audioRecordManager.startRecord();
        }
    }

    @OnClick(R.id.saveRecord)
    public void onSaveClicked() {
        save = true;
        Intent intent = new Intent();
        intent.putExtra(IntentUtils.DATA, audioRecordManager.getAudioPath());
        setResult(RESULT_OK, intent);
        finish();
    }

    @OnClick(R.id.cancel)
    public void onCancelClicked() {
        stopCount();
        finish();
    }

}
